﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WMPLib;

namespace ClassicJukebox
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : WindowWithKeypadSupport
    {
        private MediaViewModel mediaViewModel;

        public MainWindow()
        {
            InitializeComponent();
            this.mediaViewModel = new MediaViewModel(new WMPMediaLibraryRepository());
            DataContext = this.mediaViewModel;
        }

        private void WindowWithKeypadSupport_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            this.ExecuteCommandByKeyPressed(e.Key);

            if (e.Key == Key.Escape)
                Close();
            
            e.Handled = true;
        }

        private void WindowWithKeypadSupport_PreviewKeypadDown(object sender, KeypadEventArgs e)
        {        
            this.ExecuteCommandByKeyPressed(e.Key);
            e.Handled = true;
        }

        #region Helper Functions

        private void ExecuteCommandByKeyPressed(Key keyValue)
        {
            if (keyValue == Key.Left)
            {
                if (this.mediaViewModel.PreviousAlbum.CanExecute(this))
                {
                    this.mediaViewModel.PreviousAlbum.Execute(this);
                }
            }
            if (keyValue == Key.Right)
            {
                if (this.mediaViewModel.NextAlbum.CanExecute(this))
                {
                    this.mediaViewModel.NextAlbum.Execute(this);
                }
            }
            if (keyValue == Key.Up)
            {
                if (this.mediaViewModel.SongUp.CanExecute(this))
                {
                    this.mediaViewModel.SongUp.Execute(this);
                }
            }
            if (keyValue == Key.Down)
            {
                if (this.mediaViewModel.SongDown.CanExecute(this))
                {
                    this.mediaViewModel.SongDown.Execute(this);
                }
            }
            if (keyValue == Key.Enter)
            {
                if (this.mediaViewModel.PlaySong.CanExecute(this))
                {
                    this.mediaViewModel.PlaySong.Execute(this);
                }
            }
        }

        #endregion
    }
}
